package com.lb.activity.controller;

import com.lb.activity.annotation.RequestLimit;
import com.lb.activity.po.WebArticleComment;
import com.lb.activity.service.MailService;
import com.lb.activity.service.TextServiceI;
import com.lb.activity.util.ResultUtil;
import com.lb.activity.vo.ArticleVo;
import com.lb.activity.vo.MailVo;
import feign.Headers;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 001030
 */
@RestController
public class IndexController {



    @Resource
    private TextServiceI textService;

    @Autowired
    private RabbitTemplate amqpTemplate;

    @Autowired
    private MailService mailService;

    /**
     * 首页
     * @param page
     * @return
     */
    @GetMapping("/")
    @RequestLimit(count=20,time=60000)
    public ModelAndView index(Integer page, HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("index");
        //首页数据
        Map<String, Object> map = textService.restTexts(page);
        //首页置顶  top 数据
        if(page == null){
            modelAndView.addObject("curr", 1);
        }else{
            modelAndView.addObject("curr", page);
        }
        modelAndView.addObject("map", map.get("data"));
        request.getSession().setAttribute("total",map.get("total"));
        modelAndView.addObject("top", map.get("top"));
        return modelAndView;
    }

    /**
     * 详情页
     * 查看详情修改 点击查看的数量 调mq
     * @param id
     * @return
     */
    @GetMapping("show/{id}")
    @RequestLimit(count=20,time=60000)
    public ModelAndView showData(@PathVariable("id")Long id,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("show");
        ArticleVo webArticle = textService.showData(id);
        List<ArticleVo> arts = textService.webArticles();
        //发送消息
        this.amqpTemplate.convertAndSend("hello",id);
        modelAndView.addObject("webArticle",webArticle);
        modelAndView.addObject("arts",arts);
        return modelAndView;
    }

    /**
     * 同步数据到redis
     * @return
     */
    @GetMapping("sync/info")
    public String syncInfo(){
        textService.syncData();
        return "ok";
    }

    /**
     * 添加评论
     * @param webArticleComment
     * @param request
     * @return
     */
    @PostMapping("addComment")
    @RequestLimit(count=20,time=60000)
    public Object addComment(WebArticleComment webArticleComment, HttpServletRequest request){
        ResultUtil resultUtil = textService.addComment(webArticleComment);
        return resultUtil;
    }

    /**
     * 评论列表
     * @param actId
     * @return
     */
    @PostMapping("comments")
    public Object comments(@RequestParam("actId") Long actId){
        ResultUtil resultUtil = textService.comments(actId);
        return resultUtil;
    }

    /**
     * 热门评论的文章
     * @return
     */
    @PostMapping("hotCommentAct")
    public Object hotCommentAct(){
        ResultUtil resultUtil = textService.hotCommentAct();
        return resultUtil;
    }

    /**
     * 加载更多
     * @param page
     * @return
     */
    @PostMapping("loadMore")
    public Object loadMore(Integer page){
        Map<String, Object> map = textService.restTexts(page);
        String data = map.get("data") + "";
        if("[]".equals(data)){
            return ResultUtil.build(400,"没有更多数据了");
        }
        return ResultUtil.ok(map.get("data"));
    }


    /**
     * 发送邮件
     */
    @PostMapping("/mail/send")
    public MailVo sendMail(MailVo mailVo, MultipartFile[] files) {
        mailVo.setMultipartFiles(files);
        return mailService.sendMail(mailVo);
    }


}
